package kd.bos.openapi.sdk.response.result;

import kd.bos.openapi.sdk.util.StringUtils;

/**
 * 请求结果Code
 * @author johnz
 * @date 2022年4月13日
 * <p>
 * 500100 ~ 500199 基本错误模块
 * 500200 ~ 500299 权限模块
 * 500300 ~ 500399 用户模块
 * 500400 ~ 500499 商户模块
 * 500500 ~ 500599 商户模块
 */
public enum ResultCode {
    SUCCESS(0, "请求成功"),

    BUSINESS_UNKNOWN_EXCEPTION(500100, "业务逻辑异常"),

    BUSINESS_REPEAT_EXCEPTION(500101, "数据重复异常"),

    BUSINESS_NOT_EXIST_EXCEPTION(500102, "数据不存在异常"),

    BUSINESS_PARAM_NULL_EXCEPTION(500103, "必传字段为空"),

    BUSINESS_DELETE_UNAVAILABLE_EXCEPTION(500104, "不可删除"),

    BUSINESS_FAILURE_EXCEPTION(500104, "请求失败"),

    VALIDATION_EXCEPTION(500105, "字段校验不成功"),

    BUSINESS_PHONE_PATTERN_ERROR_EXCEPTION(500106, "字段校验不成功"),

    WRONG_STATUS(500107, "状态错误"),

    BUSINESS_ERROR_CODE_EXCEPTION(500301, "验证码错误"),

    BUSINESS_ACCOUNT_OR_PASSWORD_ERROR_EXCEPTION(500302, "用户名或密码错误"),

    ACCOUNT_DISABLED_EXCEPTION(500303, "用户被冻结"),

    FILE_UPLOAD_EXCEPTION(400998, "文件上传失败"),

    FILE_DOWNLOAD_EXCEPTION(400999, "文件下载失败"),

    FILE_TRANSFERTO_EXCEPTION(401000, "文件转换异常"),

    UNKNOWN_EXCEPTION(500999, "未知的系统异常"),

    MERCHANT_STATUS_ERROT_EXCEPTION(500400, "商户状态异常"),

    MERCHANT_INFO_IMPERFECT_EXCEPTION(500401, "商户信息缺失异常"),

    BUSINESS_WALLET_ACCOUNT_EXCEPTION(500410, "钱包账户错误"),

    BUSINESS_VERIFY_FAIL_EXCEPTION(500998, "验签失败"),

    BUSINESS_WALLET_ACCOUNT_EXISTS(500402, "钱包账户已经初始化"),

    BUSINESS_NOT_SUPPORT_TRANS_EXCEPTION(500501, "不支持此交易"),

    BUSINESS_TRANSCATION_EXCEPTION(500502, "交易异常"),

    ORDER_UNKNOW_EXCEPTION(500601, "订单异常");

    private int code;

    private String message;

    private ResultCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static ResultCode getResultCodeByCode(String code) {
        if (!StringUtils.isEmpty(code)) {
            return getResultCodeByCode(Integer.parseInt(code.trim()));
        }
        return null;
    }

    public static ResultCode getResultCodeByCode(int code) {
        for (ResultCode resultCode : values()) {
            if (resultCode.code == code) {
                return resultCode;
            }
        }
        return null;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public static boolean isSuccess(int type) {
        return type == ResultCode.SUCCESS.code;
    }
}
